	var pageLink = function(results, offset, title) {
		var offset = (page - 1) * PAGE_SIZE;
		return $('<a href="#"/>')
			.html(title)
			.attr('title', (offset + 1) + "...")
			.data('offset', offset)
			.click(function() {
				ajaxFetchData($div, results.query, $(this).data('offset'));
				return false;
			});
	};
	
function buildPager(results, pageLink, PAGE_SIZE, EXTRA_PAGES) {
	PAGE_SIZE = PAGE_SIZE || 10;
	EXTRA_PAGES = EXTRA_PAGES || 3;
	
	var pages = Math.floor((results.count + PAGE_SIZE - 1) / PAGE_SIZE);
	
	if(pages <= 1) {
		return '';
	}
	
	var $pager = $('<div class="pager"/>');
	var current_page = results.offset / PAGE_SIZE + 1;
	var firstPage = 1;
	var lastPage = pages;
	
	if(pages > 2*EXTRA_PAGES+1) {
		if(current_page > lastPage - EXTRA_PAGES) {
			firstPage = lastPage - 2*EXTRA_PAGES+1;
		} else if(current_page < EXTRA_PAGES + 1) {
			lastPage = 2*EXTRA_PAGES;
		} else {
			firstPage = current_page - EXTRA_PAGES + 1;
			lastPage = current_page + EXTRA_PAGES - 1;
			if(firstPage == 2) { firstPage = 1; }
			if(lastPage == pages - 1) { lastPage = pages; }
		}
	}
	
	// Previous
	if(current_page > 1) {
		$pager.append(pageLink(results, (current_page - 2) * PAGE_SIZE, "Prev"));
	} else {
		$pager.append("Prev");
	}
	$pager.append(" | ");
	
	// First
	if(firstPage > 1) {
		$pager.append(pageLink(results, 0, 1));
		$pager.append(" || ");
	}
	
	// Inner
	for(var i = firstPage; i <= lastPage; i++) {
		if(i == current_page) {
			$pager.append(i);
		} else {
			$pager.append(pageLink(results, (i-1) * PAGE_SIZE, i));
		}
		if(i < lastPage) {
			$pager.append(" | ");
		}
	}

	// Last
	if(lastPage < pages) {
		$pager.append(" || ");
		$pager.append(pageLink(results, (pages-1) * PAGE_SIZE, pages));
	}
	
	// Next
	$pager.append(" | ");
	if(current_page < pages) {
		$pager.append(pageLink(results, current_page * PAGE_SIZE, "Next"));
	} else {
		$pager.append("Next");
	}
	
	return $pager;
}
